var mysql  = require('mysql');

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    port: '3306',
    database: 'nogate'
});

connection.connect();

function demo(){
    //获取reimu列表数据
    this.query_data = function (item) {
        //var data = '';
        var  sql = 'SELECT * FROM reimu';
        //查询数据库
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            console.log('--------------------------SELECT----------------------------');
            console.log(result);
            console.log('------------------------------------------------------------\n\n');
            item(result);
        });
        //connection.end();
    };
    //获取全部的省数据
    this.province = function(item){
        var  sql = 'SELECT * FROM hat_province';
        //查询数据库
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            item(result);
        });
    };
    //获取指定的市数据
    this.city = function(cityid,item){
        var  sql = 'SELECT * FROM hat_city WHERE father=' + cityid;
        //查询数据库
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            item(result);
        });
    };
    //获取指定的区数据
    this.area = function(areaid,item){
        var  sql = 'SELECT * FROM hat_area WHERE father=' + areaid;
        //查询数据库
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            item(result);
        });
    };
}

module.exports = demo;

/*var  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
var  addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
//对数据库操作——增加一条数据
connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
        console.log('[INSERT ERROR] - ',err.message);
        return;
    }

    console.log('--------------------------INSERT----------------------------');
    //console.log('INSERT ID:',result.insertId);
    console.log('INSERT ID:',result);
    console.log('-----------------------------------------------------------------\n\n');
});*/

/*var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
var modSqlParams = ['菜鸟移动站', 'https://m.runoob.com',6];
//数据库操作——修改已经存在的数据
connection.query(modSql,modSqlParams,function (err, result) {
    if(err){
        console.log('[UPDATE ERROR] - ',err.message);
        return;
    }
    console.log('--------------------------UPDATE----------------------------');
    console.log('UPDATE affectedRows',result.affectedRows);
    console.log('-----------------------------------------------------------------\n\n');
});*/

/*var delSql = 'DELETE FROM websites where id=6';
//数据库操作——删除已经存在的数据
connection.query(delSql,function (err, result) {
    if(err){
        console.log('[DELETE ERROR] - ',err.message);
        return;
    }

    console.log('--------------------------DELETE----------------------------');
    console.log('DELETE affectedRows',result.affectedRows);
    console.log('-----------------------------------------------------------------\n\n');
});*/


//connection.end();